package com.xuecheng.content.domain.jobhandler;

import cn.hutool.core.bean.BeanUtil;
import com.xuecheng.content.domain.feignclient.SearchServiceClient;
import com.xuecheng.content.domain.model.dto.CourseBaseMarketDto;
import com.xuecheng.content.domain.model.dto.CourseIndex;
import com.xuecheng.content.domain.model.dto.CoursePreviewDto;
import com.xuecheng.content.domain.model.entity.CourseBase;
import com.xuecheng.content.domain.service.CourseBaseService;
import com.xuecheng.content.domain.service.CourseMarketService;
import com.xuecheng.content.domain.service.CoursePublishService;
import com.xuecheng.messagesdk.model.enums.MqMessageStageStateEnum;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.service.MessageProcessAbstract;
import com.xuecheng.messagesdk.service.MqMessageService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-08-31  09:56
 */
@Slf4j
@Component
public class CoursePushJob extends MessageProcessAbstract {

    @Autowired
    private CoursePublishService coursePublishService;

    @Autowired
    private CourseMarketService courseMarketService;

    @Autowired
    private SearchServiceClient searchServiceClient;

    @XxlJob("coursePushJobHandler")
    public void coursePushJob(){
        Integer shardIndex = XxlJobHelper.getShardIndex();
        Integer shardTotal = XxlJobHelper.getShardTotal();
        log.info("=================================================================");
        log.info("执行课程发布任务调度 ==> index:{}, total:{}", shardIndex, shardTotal);
        // 取出cpu核心数作为一次处理数据的条数
        Integer processors = Runtime.getRuntime().availableProcessors();
        process(shardIndex, shardTotal, processors);
    }

    @Override
    public Boolean execute(MqMessage mqMessage) {
        Long courseId = Long.parseLong(mqMessage.getBusinessKey1());
        // 生成静态化页面保存到minio
        addToMinio(mqMessage, courseId);
        // 保存到es
        addToEs(mqMessage, courseId);

        return true;
    }

    private void addToEs(MqMessage mqMessage, Long courseId) {
        MqMessageService mqMessageService = this.getMqMessageService();

        // 取出第二阶段结果做幂等性处理
        MqMessage message = mqMessageService.getById(mqMessage.getId());
        // 已经处理了就无需多此一举
        if (MqMessageStageStateEnum.SUCCESS.getCode().equals(message.getStageState2())){
            return;
        }

        // 调用搜索服务
        CourseBaseMarketDto coursePreviewDto = courseMarketService.queryCourseBaseMarket(courseId);
        searchServiceClient.addIndex(BeanUtil.copyProperties(coursePreviewDto, CourseIndex.class));
        // 更新消息步骤的状态
        mqMessageService.updateStageState(2, mqMessage.getId());
    }

    private void addToMinio(MqMessage mqMessage, Long courseId) {
        MqMessageService mqMessageService = this.getMqMessageService();

        // 取出第一阶段结果做幂等性处理
        MqMessage message = mqMessageService.getById(mqMessage.getId());
        // 已经处理了就无需多此一举
        if (MqMessageStageStateEnum.SUCCESS.getCode().equals(message.getStageState1())){
            return;
        }

        // 生成静态页面
        File file = coursePublishService.generateCourseHtml(courseId);
        if (Objects.isNull(file)){
            return;
        }
        // 保存到minio
        coursePublishService.addHtmlToMinio(file, courseId);
        // 更新消息步骤的状态
        mqMessageService.updateStageState(1, mqMessage.getId());

    }
}
